package com.android.settings.audio.peq.peq.ui.dialog;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.settings.R;
import com.android.settings.audio.peq.peq.ui.common.adapter.CommonAdapter;
import com.android.settings.audio.peq.peq.ui.common.adapter.base.ViewHolder;
import com.android.settings.audio.peq.skin.SkinManager;
import com.android.settings.audio.peq.soundeffect.SoundEffectConfig;
import com.android.settings.audio.peq.util.GetSize;

import java.util.ArrayList;
import java.util.List;

public class SoundEffectPrefabsDialog {
    private Context mContext;
    private CommanDialog mDialog;
    private View mView_Content;
    private TextView mTv_Title, mTv_Back;
    private RecyclerView recyclerView;
    private CommonAdapter<SoundEffectConfig> mAdapter;
    private List<SoundEffectConfig> _list = new ArrayList<>();

    public SoundEffectPrefabsDialog(Context context) {
        this.mContext = context;
        initDialog();
        initContentUI();
    }

    private void onDialogShow() {
    }

    private void onDialogCancel() {
    }

    private void initDialog() {
        mDialog = new CommanDialog(mContext, R.style.peqPopDialogStyle, CommanDialog.STYLE_4_HASTITLE_ONEBUTTON);
        mDialog.setOnDialogShowListener(this::onDialogShow);
        mDialog.setOnCancelDialogListener(this::onDialogCancel);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.addView(R.layout.peq_dialog_soundeffectprefabs);
        mView_Content = mDialog.getContentView();
        mTv_Title = mDialog.titleTextView;
        mTv_Back = mDialog.ok;
    }

    private void initContentUI() {
        mTv_Back.setText(R.string.peq_back);
        mTv_Back.setOnClickListener(v -> mDialog.dismiss());
        recyclerView = mView_Content.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mAdapter = new CommonAdapter<SoundEffectConfig>(mContext, R.layout.peq_item_soundeffect_dialog_text_layout, new ArrayList<>()) {
            @Override
            protected void convert(ViewHolder holder, SoundEffectConfig item, int position) {
                holder.setText(R.id.d_item, item.getName());
                holder.setChecked(R.id.cb_check, item.isChecked());
                holder.getView(R.id.cb_check).setClickable(false);
                SkinManager.get().setCheckBoxButton(holder.getView(R.id.cb_check), R.drawable.peq_skin_selector_checkbox_circle_3);
                holder.itemView.setOnClickListener(v -> {
                    if (onSaveListener != null) onSaveListener.onSave(position, item);
                });
            }
        };
        recyclerView.setAdapter(mAdapter);

        //新增预设
        mView_Content.findViewById(R.id.ll_add_prefabs).setOnClickListener(v -> {
            if (onSaveListener != null) {
                onSaveListener.onSave(-1, null);
            }
            dismiss();
        });
    }

    public void show(String title) {
        mTv_Title.setText(title);
        if (mDialog != null && !mDialog.isShowing()) {
            //adjustViewSize(mView_Content.findViewById(R.id.container_dialog_listview));
            mDialog.show();
        }
    }

    public void setData(List<SoundEffectConfig> data) {
        this._list = data;
        this.mAdapter.addData(data);
    }

    private void adjustViewSize(View view) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        int size = _list.size();
        if (size > 5) {
            layoutParams.height = GetSize.dip2px(mContext, 46) * 5;
        } else if (size > 0) {
            layoutParams.height = GetSize.dip2px(mContext, 46) * size;
        } else {
            layoutParams.height = GetSize.dip2px(mContext, 20);
        }
        view.setLayoutParams(layoutParams);
    }


    public void dismiss() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    private OnSaveListener onSaveListener;

    public void setOnSaveListener(OnSaveListener onSaveListener) {
        this.onSaveListener = onSaveListener;
    }

    public interface OnSaveListener {
        void onSave(int pos, SoundEffectConfig soundEffectConfig);
    }
}
